package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.RequiredPermission;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @RequestMapping("")
    public String index(){
        return "role_list";
    }

    @RequestMapping("/list")
    @ResponseBody
    @RequiresPermissions("role:list")
    @RequiredPermission("角色列表")
    public PageResult list(QueryObject qo){
        PageResult pageResult =  roleService.query(qo);
        return pageResult;
    }

    @RequestMapping("/listAll")
    @ResponseBody
    public List<Role> listAll(QueryObject qo){
        List<Role> roles = roleService.listAll();
        return roles;
    }
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions("role:saveOrUpdate")
    @RequiredPermission("角色新增")
    public JSONResult saveOrUpdate(Role role){
        roleService.saveOrUpdate(role);
        return new JSONResult("保存成功");
    }

    @RequestMapping("/queryPermissionsByRoleId")
    @ResponseBody
    public PageResult queryPermissionsByRoleId(Long roleId){
        List<Permission> permissions  = roleService.queryPermissionsByRoleId(roleId);
        return new PageResult(permissions.size(),permissions);
    }

    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions("role:delete")
    @RequiredPermission("角色删除")
    public JSONResult delete(Long id){
        roleService.delete(id);
        return new JSONResult("保存成功");
    }
}
